//
// Utility / Misc class
//

#ifndef CUTILITY_H
#define CUTILITY_H

typedef long int32;
typedef unsigned long uint32;
typedef float real32;

class CUtility
{
    public:

    // The radix sort could be moved to an "algorithms" namespace or class.
    // Begin helper functions and defines for radixSort
    // ================================================================================================
    // flip a float for sorting
    //  finds SIGN of fp number.
    //  if it's 1 (negative float), it flips all bits
    //  if it's 0 (positive float), it flips the sign only
    // ================================================================================================
    static inline uint32 FloatFlip(uint32 f)
    {
        uint32 mask = -int32(f >> 31) | 0x80000000;
        return f ^ mask;
    }

    static inline void FloatFlipX(uint32 &f)
    {
        uint32 mask = -int32(f >> 31) | 0x80000000;
        f ^= mask;
    }

    // ================================================================================================
    // flip a float back (invert FloatFlip)
    //  signed was flipped from above, so:
    //  if sign is 1 (negative), it flips the sign bit back
    //  if sign is 0 (positive), it flips all bits back
    // ================================================================================================
    static inline uint32 IFloatFlip(uint32 f)
    {
        uint32 mask = ((f >> 31) - 1) | 0x80000000;
        return f ^ mask;
    }

    // ---- utils for accessing 11-bit quantities
    #define _0(x)	(x & 0x7FF)
    #define _1(x)	(x >> 11 & 0x7FF)
    #define _2(x)	(x >> 22 )
    // End of helper funcs and defines for radix sort.

    static float random(); // Generate random float between 0 and 1
    static float random(float a, float b); // Generate a random float between a and b inclusive
    static std::string intToString(int i);
    static std::string floatToString(float i);

    /*
        This radix sort algo for floats was taken from
        http://www.stereopsis.com/radix.html

        I have removed the force inline and SSE prefetch parts for simplicity and portability.
        I guess I should benchmark this algo against the more usuall quicksort and not just trust the claim that its really fast.
    */
    static void RadixSort11(real32 *farray, real32 *sorted, uint32 elements);

    static float min(float a, float b);
    static void SwapNumbers(int *a, int *b);
};

#endif
